package com.weixin.paleituo.adapter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.activity.GoodsDetailsActivity;
import com.weixin.paleituo.activity.GoodsListActivity;
import com.weixin.paleituo.activity.GoodsMoreActivity;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.ChosenAdsListBean;
import com.weixin.paleituo.bean.ChosenCarouselBean;
import com.weixin.paleituo.help.Holder;
import com.weixin.paleituo.help.HolderCreator;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoopViewPager;
import com.weixin.paleituo.view.MyGridView;
import com.xiaweizi.cornerslibrary.CornersProperty;
import com.xiaweizi.cornerslibrary.RoundCornersTransformation;

import java.util.List;

/**
 * Author yichao
 * Time  2018/3/15 14:42
 * Dest  ${TODO}
 */

public class GoodsHomeAdapter extends BaseRecycleAdapter<ChosenAdsListBean> {


    public GoodsHomeAdapter(Context context, List<ChosenAdsListBean> datas) {
        super(context, datas, R.layout.item_recycleview_goodshome);
    }

    @Override
    protected void setData(RecycleViewHolder holder, final ChosenAdsListBean chosenAdsListBean, int position) {
    final MyGridView myGridView= holder.getItemView(R.id.mygrideview_goodeshome_fragment);
    ImageView ivBanner=holder.getItemView(R.id.iv_banner_item_recycleview_goodshome);
    TextView tvMore=holder.getItemView(R.id.tv_lookmore_item_recycleview_goodshome);
        tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsMoreActivity.startActivity(mContext,chosenAdsListBean.getChosenBanner().getId());
            }
        });
        ivBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsMoreActivity.startActivity(mContext,chosenAdsListBean.getChosenBanner().getId());
            }
        });

    Glide.with(mContext).load(chosenAdsListBean.getChosenBanner().getImg()).into(ivBanner);
        GoodsHomeGridAdapter goodsHomeGridAdapter=new GoodsHomeGridAdapter(mContext,chosenAdsListBean.getChosenGoodsClassList());
        myGridView.setAdapter(goodsHomeGridAdapter);
        myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        GoodsListActivity.startActivity(mContext,chosenAdsListBean.getChosenGoodsClassList().get(position).getChosenclassid()  ,1);
    }
});


        LoopViewPager viewPager= holder.getItemView(R.id.viewPager_item_recycleview_goodshome);
        viewPager.setPageMargin(UIUtils.dip2px(20));
        viewPager.setOffscreenPageLimit(3);
        GoodsHomePagerAdapter goodsHomePagerAdapter=new GoodsHomePagerAdapter(mContext, chosenAdsListBean.getChosenCarousel(), new HolderCreator<ImageViewHolder>() {
            @Override
            public ImageViewHolder createHolder() {
                return new ImageViewHolder();
            }
        });
        viewPager.setAdapter(goodsHomePagerAdapter,true);
        viewPager.startTurning(3000);


    }

    class ImageViewHolder implements Holder<ChosenCarouselBean> {
        ImageView imageView;
        @Override
        public View createView(Context context) {
            imageView   =new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

           // container.addView(imageView)

            return imageView;
        }


        @Override
        public void UpdateUI(Context context, int position, final ChosenCarouselBean data) {
           // imageView.setImageResource(R.mipmap.originality_ad);

            CornersProperty cornersProperty = new CornersProperty();
            cornersProperty.setCornersRadius(UIUtils.getDimens(R.dimen.dp_6));
            cornersProperty.setCornersType(CornersProperty.CornerType.ALL);

            RoundCornersTransformation transformation =
                    new RoundCornersTransformation(mContext,cornersProperty);
            Glide.with(context).load(data.getImg()).bitmapTransform(transformation).into(imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GoodsDetailsActivity.startActivity(mContext,data.getGoodsid());
                }
            });

        }
    }

}
